﻿package kr.pfg.pfgTracer.view.list
{
	import display.Align;
	import display.CScrollPane;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import kr.pfg.pfgTracer.model.vo.EchoClient;
	import kr.pfg.pfgTracer.view.event.VListEvent;
	
	import skinfactory.SkinButton;
	import skinfactory.SkinImage;
	
	public class VListWindow extends Sprite
	{
		private var list_BG			: Sprite;
		private var list_BG_Effect	: SkinImage;
		
		private var btnShowOn		: SkinButton;
		private var btnShowOff		: SkinButton;
		private var btnResize		: Sprite;
		
		private var itemContainer	: VListItemContainer;
		private var scrollPane		: CScrollPane;
		
		private var blnResize		: Boolean;				//Resize중인지 체크하는 변수
		
		//현재 설정된 크기 기억
		private var currentSize		: int;
		public function VListWindow()
		{
			this.init();	
		}
		private function init(): void
		{
			list_BG = new Sprite();
			list_BG.graphics.beginFill( 0xFF0000, 0 );
			list_BG.graphics.drawRect( 0, 0, 5, 8 );
			list_BG.graphics.endFill();	
			
			btnResize = new Sprite();
			btnResize.graphics.beginFill( 0xFFFFFF, 0 );
			btnResize.graphics.drawRect( 0, 0, 5, 10 );
			btnResize.graphics.endFill();
			btnResize.addEventListener( MouseEvent.MOUSE_DOWN, onResizeMouseDownListener );
			btnResize.buttonMode = true;
			btnResize.visible = false;
			blnResize = false;
			
			this.addEventListener( Event.ADDED_TO_STAGE, onAddToStageListener );
			
			itemContainer = new VListItemContainer();
			scrollPane = new CScrollPane();
			scrollPane.child = itemContainer;
			scrollPane.changeSkin( "list_scrollPane_btnUp", "list_scrollPane_btnDown", "list_scrollPane_bar", "list_scrollPane_bg" );
			scrollPane.initPos = true;
			
			btnShowOn = new SkinButton( "list_btnArrow" );
			btnShowOff = new SkinButton( "list_btnArrow" );
			btnShowOff.scaleX = -1;
			btnShowOff.visible = false;
			
			btnShowOff.addEventListener( MouseEvent.CLICK, onShowOffEventListener );
			btnShowOn.addEventListener( MouseEvent.CLICK, onShowOnEventListener );
			
			list_BG_Effect = new SkinImage( "list_bgEffect" );
			
			currentSize = 150;
			
			this.initChild();
			this.setPosition();
		}
		private function setPosition(): void
		{			
			itemContainer.x = 10;
			itemContainer.y = 4;
			
			btnShowOn.x = int( list_BG.width - btnShowOn.width );
			btnShowOn.y = Align.getCenterY( list_BG, btnShowOn );
			btnShowOff.x = int( list_BG.width );
			btnShowOff.y = Align.getCenterY( list_BG, btnShowOff );
			
			list_BG_Effect.x = list_BG.width - list_BG_Effect.width;
			list_BG_Effect.y = Align.getCenterY( list_BG, list_BG_Effect );
			
			btnResize.x = list_BG.width - btnResize.width;
			
			scrollPane.child = itemContainer;
		}
		private function initChild(): void
		{
			this.addChild( list_BG );
			this.addChild( list_BG_Effect );
			this.addChild( scrollPane );
			this.addChild( btnResize );
			this.addChild( btnShowOff );
			this.addChild( btnShowOn );
		}
		override public function set width(value:Number):void
		{	
			list_BG.width 		= value;
			itemContainer.width = value - 11;
			scrollPane.width = value - 11;
			
			this.setPosition();		
		}
		override public function set height(value:Number):void
		{
			list_BG.height 		= value;
			btnResize.height 	= value;
			scrollPane.height 	= value;
			
			this.setPosition();
		}
		override public function get width():Number
		{
			return list_BG.width;
		}
		
		public function addItem ( data: EchoClient ): void
		{
			itemContainer.addItem( data );
			scrollPane.child = itemContainer;
		}
		
		private function onShowOffEventListener( e: MouseEvent ): void
		{
			btnShowOff.visible = false;
			btnShowOn.visible = true;
			btnResize.visible = false;
			scrollPane.visible = false;
			
			this.dispatchEvent( new VListEvent( VListEvent.SHOW_OFF ) );
		}
		private function onShowOnEventListener( e: MouseEvent ): void
		{
			btnShowOff.visible = true;
			btnShowOn.visible = false;
			btnResize.visible = true;
			scrollPane.visible = true;
			
			this.dispatchEvent( new VListEvent( VListEvent.SHOW_ON, currentSize ) );
		}
		
		/**▧▧▧ About Resize ▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧**/
		private function onAddToStageListener( e: Event ): void
		{
			this.stage.addEventListener( MouseEvent.MOUSE_UP, onResizeMouseUpListener );
		}
		private function onResizeMouseDownListener( e: MouseEvent ): void
		{
			blnResize = true;
			this.addEventListener( Event.ENTER_FRAME, onEnterFrameListener );
		}
		private function onEnterFrameListener( e: Event ): void
		{
			var size: int = mouseX;
			if( size < 50 )
				size = 50; 
			this.dispatchEvent( new VListEvent( VListEvent.SHOW_ON, size ) );
		}
		private function onResizeMouseUpListener( e: MouseEvent ): void
		{
			if( blnResize )
			{
				currentSize = mouseX;
				blnResize = false;
			}
			this.removeEventListener( Event.ENTER_FRAME, onEnterFrameListener );
		}
	}
}